package cn.ssq.controller;

import cn.ssq.domain.CropCategory;
import cn.ssq.domain.CropCpecies;
import cn.ssq.domain.Result;
import cn.ssq.service.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/SampleInfoController")
public class SampleInfoController {

    @Autowired
    private SampleInfoService sampleInfoService;
    @Autowired
    private SampleToxinService sampleToxinService;
    @Autowired
    private CropCategoryService cropCategoryService;
    @Autowired
    private CropCpeciesService cropCpeciesService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private SampleToxinInfoService sampleToxinInfoService;
    @Autowired
    private BacterialStrainfoService bacterialStrainfoService;


    /**
     * 添加样品信息
     * @param map
     * @param toxinId
     * @param toxinCount
     * @param original_num
     * @param word_addr
     * @param txt_addr
     * @param picture_addr
     * @return
     */
    @RequestMapping("/addSampleInfo")
    @ResponseBody
    public Result addSampleInfo(@RequestParam Map<String, Object> map  , Integer[] toxinId, String[] toxinCount, String[] original_num, String[] word_addr, String[] txt_addr, String[]picture_addr) {

        System.out.println(map);
        int id = sampleInfoService.addSampleInfo(map);
        int i1 = sampleToxinService.insertTxon(id,toxinId,toxinCount);
       // int i2 = (int) bacterialStrainfoService.addBacterialStrainfo(map,original_num,word_addr,txt_addr,picture_addr);
        Result result = new Result();
        /*if (i+i1+i2>0){
            result.setFlag(true);
        }else {
            result.setFlag(false);
        }*/
        return result;
    }

    /**
     * 查询所有的农农产品原料
     *
     * @param
     * @return
     */
    @RequestMapping("/findByCropCategory")
    @ResponseBody
    private String findByCropCategory() {
        List<CropCategory> categoryList = cropCategoryService.findByCropCategory();
        List<CropCpecies> cropCpeciesList = cropCpeciesService.findCropCpeciesById();
        Map map = new HashMap();
        map.put("categoryList", categoryList);
        map.put("cropCpeciesList", cropCpeciesList);
        return JSON.toJSONString(map);
    }

    /**
     * 查询省市级
     *
     * @param
     * @return
     */
    @RequestMapping("/selectAddress")
    @ResponseBody
    private String selectAddress() {
        Map map = addressService.selectAddress();
        return JSON.toJSONString(map);
    }

    /**
     *
     * 校验样品编号
     * @param sampleInfoId
     * @return
     */
    @RequestMapping("/verifyUser")
    @ResponseBody
    private Result verifyUser(String sampleInfoId){
        System.out.println(sampleInfoId);
        Result result = new Result();
        String[] selectverifyUser = sampleInfoService.selectverifyUser();
        System.out.println(selectverifyUser);
        for (String s : selectverifyUser) {
            if (sampleInfoId.equals(s)){
                result.setFlag(true);
            }
        }
        return result;
    }

    /**
     * 上传文件
     * @param
     * @param word_addr
     * @param txt_addr
     * @param picture_addr
     * @param toxinId
     * @param toxinCount
     * @param original_num
     * @return
     */
    @RequestMapping(value = "/addSample",method= RequestMethod.POST)
    @ResponseBody
    public Result addSample(@RequestParam Map<String, Object> map , @RequestParam(value = "word_addr", required = false) MultipartFile[] word_addr, @RequestParam(value = "txt_addr", required = false) MultipartFile[] txt_addr, @RequestParam(value = "picture_addr", required = false) MultipartFile[] picture_addr
            , Integer[] toxinId, String[] toxinCount, String[] original_num) {
        System.out.println(map);
        int id = sampleInfoService.addSampleInfo(map);
        int i1 = sampleToxinService.insertTxon(id,toxinId,toxinCount);
        String wordsuffer = "";
        String txtsuffer = "";
        String picsuffer = "";

        //分别用来存放三个集合上传完成之后的路径
        List<String> wordStrings = new ArrayList<String>();
        List<String> txtStrings = new ArrayList<String>();
        List<String> picStrings = new ArrayList<String>();


        //循环获取三个上传集合的后缀名称
        for (int i=0;i<word_addr.length;i++){
            //获取上传文件的名称
            wordsuffer =  word_addr[i].getOriginalFilename().substring(word_addr[i].getOriginalFilename().lastIndexOf("."));
            txtsuffer =  txt_addr[i].getOriginalFilename().substring(txt_addr[i].getOriginalFilename().lastIndexOf("."));
            picsuffer =  picture_addr[i].getOriginalFilename().substring(picture_addr[i].getOriginalFilename().lastIndexOf("."));

            //创建一个上传的文件路径
            String path = "D:/myfile/";

            long l = System.currentTimeMillis();
            String s = String.valueOf(l);

            //拼接上传的全路径
            String finalWordPath = path+s+wordsuffer;
            String finalTxtPath = path+s+txtsuffer;
            String finalPicPath = path+s+picsuffer;

            wordStrings.add(finalWordPath);
            txtStrings.add(finalTxtPath);
            picStrings.add(finalPicPath);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
        }
        for (int j=0;j<word_addr.length;j++){
            try {
                //进行上传操作
                word_addr[j].transferTo(new File(wordStrings.get(j)));
                txt_addr[j].transferTo(new File(txtStrings.get(j)));
                picture_addr[j].transferTo(new File(picStrings.get(j)));
                //循环把上传完成之后的路径存到list集合里面
            } catch (IOException e) {
                e.printStackTrace();
                System.err.println(e);
            }
        }
        List<String> strings = Arrays.asList(original_num);
        int i2 = bacterialStrainfoService.addBacterialStrainfo(id,strings,wordStrings,txtStrings,picStrings);
        Result result = new Result();
        if (i1+i2>0){
            result.setFlag(true);
        }else {
            result.setFlag(false);
        }
        return result;
    }

}
